﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace DbWord
{
    sealed class ParametersCache
    {
        static readonly ParametersCache instance = new ParametersCache();
        public static ParametersCache Instance
        {
            get { return instance; }
        }
        ParametersCache()
        {
            helper = new Request.Helper();
        }

        public void InitArgs(DateTime dt)
        {
            ParametersDic = InitParametersDic(dt);
            NormalSqlDic = InitNormalXElementDic(dt);
            ChartSqlDic = InitChartXElementDic(dt);
        }

        Request.Helper helper { get; set; }
        /// <summary>
        /// 初始化用的时间
        /// </summary>
        public DateTime InitDateTime { get; set; }
        public Dictionary<string, DateTime> ParametersDic { get; set; }
        public Dictionary<string, XElement> NormalSqlDic { get; set; }
        public Dictionary<string, XElement> ChartSqlDic { get; set; }

        Dictionary<string, DateTime> InitParametersDic(DateTime dt)
        {
            XDocument doc = XDocument.Load(Defs.XmlDocFile);
            var sdic = (from c in doc.Descendants("Parameter").Where(p => p.Attribute("src").Value == "sql")
                        select new
                        {
                            Key = c.Element("Name").Value.Trim(),
                            Sql = c.Element("Sql").Value.Trim()
                        });
            var dic = sdic.Select(o =>
            {
                var newp = new
                    {
                        Key = o.Key,
                        Val = helper.Idb.ExecuteScalar(string.Format(o.Sql,dt))
                    };
                return newp;
            }).ToDictionary(o => o.Key, o => Convert.ToDateTime(o.Val));

            return dic;
        }
        Dictionary<string, XElement> InitNormalXElementDic(DateTime dt)
        {
            XDocument doc = XDocument.Load(Defs.XmlDocFile);
            var sdic = (from c in doc.Descendants("PlaceHolder")
                        select new
                        {
                            Key = c.Element("Name").Value.Trim(),
                            Node = c
                        });
            var dic = sdic.Select(o =>
            {
                var newp = new
                    {
                        Key = o.Key,
                        Val = o.Node
                    };
                return newp;
            }).ToDictionary(o => o.Key, o => o.Val);

            return dic;
        }
        Dictionary<string, XElement> InitChartXElementDic(DateTime dt)
        {
            XDocument doc = XDocument.Load(Defs.XmlDocFile);
            var sdic = (from c in doc.Descendants("PlaceHolder")
                        select new
                        {
                            Key = c.Element("Name").Value.Trim(),
                            Node = c
                        });
            var dic = sdic.Select(o =>
            {
                var newp = new
                    {
                        Key = o.Key,
                        Val = o.Node
                    };
                return newp;
            }).ToDictionary(o => o.Key, o => o.Val);

            return dic;
        }
    }
}
